# config.py

import json

import json5

# 全局变量
CONFIG={}

def load_config(file_path: str='config.json5') -> dict:
    """读取 JSON5 文件并转换为 Python 字典"""
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            global CONFIG
            CONFIG = json5.load(file)  # 将 JSON5 解析为字典
            return CONFIG
    except FileNotFoundError:
        print(f"错误：文件 '{file_path}' 不存在")
        return {}
    except json5.JSON5DecodeError as e:
        print(f"JSON5 解析错误：{e}")
        return {}
    except Exception as e:
        print(f"其他错误：{e}")
        return {}
if __name__ == '__main__':
    CONFIG = load_config('config.json5')
    print(CONFIG.get('dev')['mysql']['host'])

    if CONFIG:
        print("成功读取并转换为字典：")
        for key, value in CONFIG.items():
            print(f"{key}: {value}")